using Digitalmes.Application.Mes.Masters.Workstations;
using Digitalmes.Application.Mes.Masters.Workstations.Commands;
using Digitalmes.Application.Mes.Masters.Workstations.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Masters;

/// <summary>
/// 工站终结点。
/// </summary>
public sealed class WorkstationEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/master", "workstation")
            .RequireAuthorization()
            .MapGet(GetWorkstationListAsync, "list")
            .MapGet(GetWorkstationPagedListAsync, "list-page")
            .MapPost(CreateWorkstationAsync)
            .MapPut(UpdateWorkstationAsync, "{id}")
            .MapDelete(DeleteWorkstationAsync, "{id}");
    }

    [EndpointDescription("查询工位集合")]
    public async Task<IApiResult> GetWorkstationListAsync(ISender sender, [AsParameters] GetWorkstationListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工位")]
    public async Task<IApiResult> GetWorkstationPagedListAsync(ISender sender, [AsParameters] GetWorkstationListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建工位")]
    public async Task<IApiResult> CreateWorkstationAsync(ISender sender, MdWorkstationDto dto)
    {
        var result = await sender.Send(new CreateWorkstationCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新工位")]
    public async Task<IApiResult> UpdateWorkstationAsync(ISender sender, long id, MdWorkstationDto dto)
    {
        var result = await sender.Send(new UpdateWorkstationCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工位")]
    public async Task<IApiResult> DeleteWorkstationAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteWorkstationCommand(id));
        return result.ToApiResult();
    }
}
